<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
use Admin\Model\Goods;
class GoodsController extends CommonController{
    private $model;
    public function __init(){
        //调用父级的构造函数
        parent::__init();
        //实例化商品表
        $this->model = new Goods;
    }
    //载入模板
    public function index(){
        //数据总数
        $count = $this->model->count();
        //每10条数据分一页
        $page = Page::row(10)->make($count);
        $data = $this->model->limit(Page::limit())->get();
        View::with('page',$page);
        View::with('data',$data);
        View::make();
    }
    //增加
    public function add(){
      if(IS_POST){
            //获得商品表内容
        $data['gname']=Q('post.gname');
        $data['inventory']=Q('post.inventory');
        $data['unit']=Q('post.unit');
        $data['market_price']=Q('market_price');
        $data['type_tid']=Q('type_tid');
        $data['shop_price']=Q('shop_price');
        $data['list_view'] =Q('list_view');
        $data['click'] =Q('click');
        $data['category_cid']=Q('category_cid');
        $data['brand_bid'] =Q('brand_bid');
        $data['manager_id']=Q('id');
        $data['addtime']=time();
            //获得商品表添加成功后返回的gid
        $gid=$this->model->store($data);
        if(!$gid){
            View::error($this->model->getError());
            return false;
        }
            //商品表结束
        //获得属性
        $gmodel = new \Admin\Model\Goods_attr;
        foreach((Q('post.attr',array())) as $tat => $gavalue){
            $gst['gavalue'] = $gavalue;
            $gst['type_attribute_taid']= $tat;
            $gst['goods_gid']=$gid;
            $gmodel->add($gst);
        }
        //获得规格
        $spec = Q('post.spec');
        foreach($spec as $tat => $v){
            $gst['type_attribute_taid'] =$tat;
            $gst['goods_gid']= $gid;
            foreach($v['value'] as $k=>$val){
                $gst['gavalue'] =$val;
                $gst['value_added'] = $v['price'][$k];
                $gmodel->add($gst);
            }
        }
            //属性表结束
            //详细表
        $gdmodel= new \Admin\model\Goods_detail;
        $album = Q('post.album');
        $album = implode(',', $album);
        $detail = array(
            'goods_detail'=>Q('post.content'),
            'after_sale'=>Q('post.intro'),
            'goods_info'=>Q('post.goods_info'),
            'goods_gid'=>$gid,
            'album'=>$album
        );
        $gdmodel->add($detail); 
        View::success('添加成功',U('index')); 
      }

        //获取所有的分类
        $cateData = Db::table("category")->get();
        $cateData = Data::tree($cateData,'cname');
        $brand = Db::table("brand")->get();
        View::with('brand',$brand);
        View::with('cateData',$cateData);
        View::make();
    }
    public function ajaxGetAttr(){
        if(IS_AJAX){
            //通过类型id获得类型属性
            $type_tid = Q('post.type_tid',0,'intval');
            $data = Db::table('type_attribute')->where("type_tid={$type_tid}")->get();
            //如果有类型属性
            foreach ((array)$data as $k => $v) {
                //把tavalue 字符串变为数组
                $data[$k]['tavalue'] = explode('|',$v['tavalue']);
            }
            echo json_encode($data);
            exit;
        }
    }
    //上传
    public function upload(){
        //框架上传方法
        $file = Upload::path('Upload/Content/' . date('y/m'))->make();
        //如果上传失败
        if (empty($file)) {
            echo json_encode(Upload::getError());
        } else {
            /** $file内部就是以下这个数组
                $file = array(
                    0 => array(
                    'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
                    'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
                    'image' => 1
                ),
            );**/
            //上传成功，把上传好的信息返给js
            $data = $file[0]['path'];
            echo json_encode($data);exit;
        }
    }
    public function uploadMore(){
        //框架上传方法
        $file = Upload::path('Upload/Content/' . date('y/m'))->make();
        $uploadDir='Upload/album/'.date('y/m');
        //如果上传失败
        if (empty($file)) {
            echo json_encode(Upload::getError());
        } else {
            $data = $file[0];
            // 缩略(1.上传图片的路径，2.缩略图的名字，3.width,4.height,5.缩略的方式)
            Image::thumb($file[0]['path'],$uploadDir .'/s_'.$file[0]['basename'],50,50,1);
            Image::thumb($file[0]['path'],$uploadDir .'/m_'.$file[0]['basename'],480,480,1);
            Image::thumb($file[0]['path'],$uploadDir .'/b_'.$file[0]['basename'],800,800,1);
            //上传成功，把上传好的信息返给js
            
            echo json_encode($data);exit;
        }
    }
    //删除
    public function del(){
        $gid = Q('get.gid');
        if(!$this->model->del($gid)){
            View::error($this->model->getError());
            return false;
        }
        View::success('删除成功',U('index'));
    }
    //编辑
    public function edit(){
        $gid = Q('get.gid',0,'intval');
        Db::table("goods_list")->where("goods_gid={$gid}")->delete();
        if(IS_POST){
            // sp($_POST);die;
                //获得商品表内容
            $data['gname']=Q('post.gname');
            $data['inventory']=Q('post.inventory');
            $data['unit']=Q('post.unit');
            $data['market_price']=Q('market_price');
            $data['type_tid']=Q('type_tid');
            $data['shop_price']=Q('shop_price');
            $data['list_view'] =Q('list_view');
            $data['click'] =Q('click');
            $data['category_cid']=Q('category_cid');
            $data['brand_bid'] =Q('brand_bid');
            $data['manager_id']=Q('id');
            $data['addtime']=time();
                //获得商品表添加成功后返回的gid
            if(!$this->model->edit($gid)){
                View::error($this->model->getError());
                return false;
            }
                //商品表结束
                //获得属性
            $gmodel = new \Admin\Model\Goods_attr;
            $gmodel->where("goods_gid={$gid}")->delete();
            foreach((Q('post.attr',array())) as $tat => $gavalue){
                $gst['gavalue'] = $gavalue;
                $gst['type_attribute_taid']= $tat;
                $gst['goods_gid']=$gid;
                $gmodel->where("goods_gid={$gid}")->add($gst);
            }
            //获得规格
            $spec = Q('post.spec',array());
            // sp($spec);die;
            foreach($spec as $tat => $v){
                $gst['type_attribute_taid'] =$tat;
                $gst['goods_gid']= $gid;
                foreach($v['value'] as $k=>$val){
                    $gst['gavalue'] =$val;
                    $gst['value_added'] = $v['price'][$k];
                    $gmodel->where("goods_gid={$gid}")->add($gst);
                }
            }
                //属性表结束
                //详细表
            $gdmodel= new \Admin\model\Goods_detail;
            $album = Q('post.album');
            $album = implode(',', $album);
            $detail = array(
                'goods_detail'=>Q('post.content'),
                'after_sale'=>Q('post.intro'),
                'goods_gid'=>$gid,
                'album'=>$album
            );
            $gdmodel->where("goods_gid={$gid}")->save($detail);
            View::success('修改成功',U('index'));
        }
        $data = $this->model->where("gid={$gid}")->get();
        View::with('data',$data[0]);
            //获得属性
        $gmodel = new \Admin\Model\Goods_attr;
            //获取属性
        $gdata = $gmodel->join("type_attribute","type_attribute_taid","=","taid")->where("goods_gid={$gid} AND class=1")->get();
        foreach ($gdata as $k=>$v){
            $tavalue =$v['tavalue'];
                //把属性转为数组
            $tavalue = explode('|', $tavalue);
                //把数组压入属性
            $gdata[$k]['tavalue']=$tavalue;
        }
            //获得规格
        $gdata2 = $gmodel->join("type_attribute","type_attribute_taid","=","taid")->where("goods_gid={$gid} AND class=2")->get();
        foreach ($gdata2 as $k=>$v){
            $tavalue = $v['tavalue'];
              //把规格转为数组
            $tavalue = explode('|', $tavalue);
                 //把数组压入属性规格
            $gdata2[$k]['tavalue']=$tavalue;
        }
        // sp($gdata2);die;         
        View::with('gdata',$gdata);
        View::with('gdata2',$gdata2);
            //获得详细
        $gdmodel= new \Admin\model\Goods_detail;
        $gddata = $gdmodel->where("goods_gid={$gid}")->get();
        $album = $gdmodel->where("goods_gid={$gid}")->pluck('album');
        $album = explode(',',$album);
        View::with('album',$album);
        View::with('gddata',$gddata[0]);
            //获取所有分类
        $cateData = Db::table("category")->get();
        $cateData = Data::tree($cateData,'cname');
            //获取所有品牌
        $brand = Db::table("brand")->get();
        View::with('brand',$brand);
        View::with('cateData',$cateData);
        View::make();
    }





}


?>